package com.cfg;

import com.cfg.annotation.ServiceDynamicProxy;
import com.cfg.annotation.ServiceMethodDynamicProxy;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author <a href="15226116300@163.com">王廷俊</a>
 * @Description
 * @Date 2024-11-12
 */
public class DynamicProxyConfiguration {
    
    protected String[] scanPackages = null;
    
    protected Map<String, Map<String, List<Class<?>>>> proxyCfg;
    
    private static DynamicProxyConfiguration cdps;
    
    public static synchronized DynamicProxyConfiguration getInstance() {
        if (cdps == null) {
            cdps = new DynamicProxyConfiguration();
            cdps.scanPackages = new String[] {"com.cfg.demo"};
            cdps.initProxyConfig();
        }
        
        return cdps;
    }
    
    protected boolean initProxyConfig() {
        for (int var1 = 0; var1 < this.scanPackages.length; ++var1) {
            this.initProxyConfig(this.scanPackages[var1]);
        }
        
        this.writeProxyInitLog();
        return true;
    }
    
    protected void initProxyConfig(String var1) {
        Set var2 = PackageUtil.scanClass4Package(var1, false);
        Iterator var3 = var2.iterator();
        
        while (var3.hasNext()) {
            Class var4 = (Class) var3.next();
            Annotation[] var5 = var4.getAnnotations();
            Annotation[] var6 = var5;
            int var7 = var5.length;
            
            for (int var8 = 0; var8 < var7; ++var8) {
                Annotation var9 = var6[var8];
                Class var10 = var9.getClass();
                String var11 = null;
                Class var12 = null;
                String var13 = null;
                if (var9 instanceof ServiceDynamicProxy) {
                    try {
                        var12 = ((ServiceDynamicProxy) var9).target();
                    } catch (Exception var20) {
                        System.err.println("The proxy class does not exist. See the following stack for details!\r\n"
                                + var20.getMessage());
                        var20.printStackTrace();
                        continue;
                    }
                    
                    if (var12 != null) {
                        this.getProxyList("service", var12.getName()).add(var4);
                        Method[] var22 = var4.getMethods();
                        Method[] var15 = var22;
                        int var16 = var22.length;
                        
                        for (int var17 = 0; var17 < var16; ++var17) {
                            Method var18 = var15[var17];
                            if (var18.isAnnotationPresent(ServiceMethodDynamicProxy.class)) {
                                var13 = PackageUtil.getMethodSignature(var12, var18);
                                List var19 = this.getProxyList("serviceMethod", var13);
                                var19.add(var4);
                            }
                        }
                    }
                }
            }
        }
        
    }
    
    private void writeProxyInitLog() {
        System.out.println("Loading business class(SERVICE-METHOD) dynamic proxy...");
        System.out.println(this.getProxyLog((Map) this.proxyCfg.get("serviceMethod")));
        System.out.println("The business class dynamic proxy is loaded.");
    }
    
    protected String getProxyLog(Map<String, List<Class<?>>> var1) {
        if (var1 == null) {
            return null;
        } else {
            StringBuilder var2 = new StringBuilder();
            Set var3 = var1.entrySet();
            Iterator var4 = var3.iterator();
            
            while (var4.hasNext()) {
                Map.Entry var5 = (Map.Entry) var4.next();
                String var6 = (String) var5.getKey();
                List var7 = (List) var5.getValue();
                var2.append("\r\n>> target：");
                var2.append(var6);
                var2.append("\r\n>> proxy：");
                
                for (int var8 = 0; var8 < var7.size(); ++var8) {
                    var2.append("->");
                    var2.append(((Class) var7.get(var8)).getName());
                }
            }
            
            return var2.toString();
        }
    }
    
    public Map<String, Map<String, List<Class<?>>>> getProxyCfg() {
        return this.proxyCfg;
    }
    
    public void setProxyCfg(Map<String, Map<String, List<Class<?>>>> var1) {
        this.proxyCfg = var1;
    }
    
    public ProxyInterface getFirstPorxyObject(String typeString, String targetName)
            throws IllegalAccessException, InstantiationException {
        if (proxyCfg != null) {
            Map<String, List<Class<?>>> cfg = proxyCfg.get(typeString);
            if (cfg != null) {
                List<Class<?>> targetProxys = cfg.get(targetName);
                if (targetProxys != null && !targetProxys.isEmpty()) {
                    ProxyInterface firstCommandProxy = (ProxyInterface) targetProxys.get(0).newInstance();
                    ProxyInterface prevCommandProxy = firstCommandProxy;
                    ProxyInterface lastCommandProxy = firstCommandProxy;
                    for (int i = 0; i < targetProxys.size() - 1; i++) {
                        ProxyInterface proxyObject = (ProxyInterface) targetProxys.get(i + 1).newInstance();
                        prevCommandProxy.setNextProxy(proxyObject);
                        prevCommandProxy = proxyObject;
                    }
                    firstCommandProxy.setLastProxy(prevCommandProxy);
                    return firstCommandProxy;
                }
            }
        }
        return null;
    }
    
    public Class<?> getPorxyClass(String var1, String var2) {
        if (this.proxyCfg != null) {
            Map var3 = (Map) this.proxyCfg.get(var1);
            if (var3 != null) {
                List var4 = (List) var3.get(var2);
                if (var4 != null && !var4.isEmpty()) {
                    return (Class) var4.get(0);
                }
            }
        }
        
        return null;
    }
    
    protected DynamicProxyConfiguration() {
        proxyCfg = new HashMap<String, Map<String, List<Class<?>>>>();
    }
    
    public List<Class<?>> getProxyList(String var1, String var2) {
        Object var3 = (Map) this.proxyCfg.get(var1);
        if (var3 == null) {
            var3 = new HashMap();
            this.proxyCfg.put(var1, (Map<String, List<Class<?>>>) var3);
        }
        
        Object var4 = (List) ((Map) var3).get(var2);
        if (var4 == null) {
            var4 = new ArrayList();
            ((Map) var3).put(var2, var4);
        }
        
        return (List) var4;
    }
}